package com.android.aircondition.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.android.aircondition.R;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Temperature;
import com.android.aircondition.utils.CommonUtils;

public class TemperatureActivity extends BaseActivity{
	
	private String upPeriod;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperature);
        super.setCommonTitle(this, R.string.title_temperature);
        
        upPeriod = getText(R.string.unit_up_period).toString();
        
    }
    
    protected void onResume() {
    	super.onResume();
    	setViews();
    }
    
    private void setViews(){
    	
    	final ImageButton increateBtn  = (ImageButton) findViewById(R.id.temperatureIncreaseImageButton);
    	final ImageButton reduceBtn    = (ImageButton) findViewById(R.id.temperatureReduceImageButton);
    	final TextView unit_C_TextView = (TextView)findViewById(R.id.unit_C_TextView);
    	final TextView unit_F_TextView = (TextView)findViewById(R.id.unit_F_TextView);
    	final TextView temperatureTextView = (TextView)findViewById(R.id.temperatureTextView);
    	final LinearLayout temperatureSwithLinear = (LinearLayout)findViewById(R.id.temperatureSwithLinear);
    	final SeekBar  temperatureSeekbar = (SeekBar)findViewById(R.id.temperatureSeekBar);
    	
    	Temperature temperature = new GreeService(this).getTemperaturePreference();
    	
    	temperatureSeekbar.setProgress((((int)temperature.getUnit_F()/1)-61)*10);
    	
    	if(temperature.iscUnitKey()){
    		temperatureSwithLinear.setBackgroundResource(R.drawable.swith_on);
			unit_C_TextView.setText(R.string.unit_C);
			temperatureTextView.setText(new StringBuffer()
										   .append(CommonUtils.halfRound(temperature.getUnit_C()))
										   .append(upPeriod)
										   .append("C"));
    	}else{
    		temperatureSwithLinear.setBackgroundResource(R.drawable.swith_on_2);
			unit_F_TextView.setText(R.string.unit_F);
			temperatureTextView.setText(new StringBuffer().append(temperature.getUnit_F())
														  .append(upPeriod)
					  									  .append("F"));
    	}
    	
    	   
        OnClickListener mListener = new OnClickListener() {
    		
    		public void onClick(View v) {
    			GreeService service = new GreeService(getApplicationContext());
    			Temperature temperature = service.getTemperaturePreference();
    			int temp = (int)temperature.getUnit_F();
				int progress = (temp -61)*10;
    			
    			switch (v.getId()) {
    			
    			case R.id.unit_C_TextView:
    				temperatureSwithLinear.setBackgroundResource(R.drawable.swith_on);
    				unit_C_TextView.setText(R.string.unit_C);
    				temperatureTextView.setText(new StringBuffer()
    												.append(CommonUtils.halfRound(temperature.getUnit_C()))
    												.append(upPeriod)
    									     		.append("C"));
    				temperature.setcUnit(true);
    				
    				break;
    				
    			case R.id.unit_F_TextView:
    				temperatureSwithLinear.setBackgroundResource(R.drawable.swith_on_2);
    				unit_F_TextView.setText(R.string.unit_F);
    				temperatureTextView.setText(new StringBuffer()
    				                                .append(temperature.getUnit_F())
    				                                .append(getText(R.string.unit_up_period))
							  						.append("F"));
    				temperature.setcUnit(false);
    				break;
    			case R.id.temperatureIncreaseImageButton:
    				progress += 10;
    			    temperatureSeekbar.setProgress(progress);
    				temperature.setUnit_F(61+progress/10);
    				break;
    			case R.id.temperatureReduceImageButton:
    				progress -= 10;
    			    temperatureSeekbar.setProgress(progress);
    			    temperature.setUnit_F(61+progress/10);
    				break;

    			}
    			
    			service.setTemperaturePreference(temperature);
    		}
    	};
    	
    	unit_C_TextView.setOnClickListener(mListener);
    	unit_F_TextView.setOnClickListener(mListener);
    	increateBtn.setOnClickListener(mListener);
    	reduceBtn.setOnClickListener(mListener);
    	
    	
    	temperatureSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    		
    		private Temperature temperature;
    		private GreeService service = new GreeService(getApplicationContext());
			
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				service.setTemperaturePreference(temperature);
			}

			public void onStartTrackingTouch(SeekBar seekBar) {

				temperature =  service.getTemperaturePreference();

			}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				
				if(service  == null){
					service = new GreeService(getApplicationContext());	
				}
				
				temperature =  service.getTemperaturePreference();

				float f = 61 + progress/10;
					  
				if(temperature.iscUnitKey()){
					
					temperature.setUnit_F(f);

					temperatureTextView.setText(new StringBuffer()
									                .append(CommonUtils.halfRound(temperature.getUnit_C()))
									                .append(upPeriod)
							  		                .append("C"));
				}else{
					
					temperature.setUnit_F(f);
					
					temperatureTextView.setText(new StringBuffer()
													.append(f)
													.append(upPeriod)
													.append("F"));
					
				}
				
			}
		});
    	
    }
    
}
